define(function () {
    'use strict';

    function ctrl($scope, $state, $ionicPopup, CommonService, LocalCacheService) {
        function init() {
            $scope.financingPledge = {
                needMarketValue: 122000.00,
                allCounterFee: 0,
                allRate: 0,
                choseMarketValue: 0
            };
            $scope.sharesList = [{
                name: '中国联通 600060',
                count: 125000,
                price: 125.00,
                marketValue: 0,
                rate: 60,
                counterFee: 15.50,
                value: 0,
                allCheck: false,
                checked: false
            }, {
                name: '中国联通 600060',
                count: 125000,
                price: 125.00,
                marketValue: 0,
                rate: 60,
                counterFee: 15.50,
                value: 0,
                allCheck: false,
                checked: false
            }]
        }

        $scope.$on('$ionicView.loaded', function () {
            init();
        });

        function allValue() {
            var allValue = 0;
            for (var i = 0; i < $scope.sharesList.length; i++) {
                if ($scope.sharesList[i].checked) {
                    allValue += $scope.sharesList[i].price * $scope.sharesList[i].value;
                }
            }
            $scope.financingPledge.choseMarketValue = allValue;
        }

        $scope.check = function () {
            allValue();
        };

        $scope.plus = function (index) {
            $scope.sharesList[index].value += 100;
            $scope.sharesList[index].marketValue = $scope.sharesList[index].value * $scope.sharesList[index].price;
            allValue();
        };

        $scope.minus = function (index) {
            $scope.sharesList[index].value -= 100;
            $scope.sharesList[index].marketValue = $scope.sharesList[index].value * $scope.sharesList[index].price;
            allValue();
        };

        $scope.all = function (index) {
            if ($scope.sharesList[index].allCheck) {
                $scope.sharesList[index].value = $scope.sharesList[index].count;
            } else {
                $scope.sharesList[index].value = 0;
            }
            $scope.sharesList[index].marketValue = $scope.sharesList[index].value * $scope.sharesList[index].price;
            allValue();
        };

        $scope.show = function () {
            $ionicPopup.show({
                cssClass: "kDialog",
                template: "手续费=经手费+质押登记费。经手费由交易所收取，收费标准为：沪市按融资金额的0.01‰收取，起点5元，最高100元；深市按每股面值的1‰收取，最高不超过100元。质押登记费由登记公司收取，收费标准为：500万股以下（含）部分按该部分面值的1‰收取，超500万股的部分按该部分面值的0.1‰收取，起点100元。",
                buttons: [{text: '我知道了'}]
            });
        };

        $scope.submit = function () {
            $state.go('tab.account-easyFinancingRepayment');
        }
    }

    ctrl.$inject = ['$scope', '$state', '$ionicPopup', 'CommonService', 'LocalCacheService'];
    return ctrl;
});
